function SetAllCheckBoxes(FormName, FieldName, CheckValue) {
	if(!document.forms[FormName])
		return;
	var objCheckBoxes = document.forms[FormName].elements[FieldName];
	if(!objCheckBoxes)
		return;
	var countCheckBoxes = objCheckBoxes.length;
	if(!countCheckBoxes)
		objCheckBoxes.checked = CheckValue;
	else
		// set the check value for all check boxes
		for(var i = 0; i < countCheckBoxes; i++)
			objCheckBoxes[i].checked = CheckValue;
}

function AddTheTag(Tag) {
	if(document.editor.tags.value == '')
		document.editor.tags.value += Tag;
	else
		document.editor.tags.value += ', ' + Tag;
}

function CheckTheUrl(Url) {
	var httpregex = new RegExp("^http://(.*)?");
	var match = httpregex.exec(Url);
	if(match == null) {
		$('url').value = "http://" + Url;
		var url = 'http://' + Url;
	} else {
		var url = Url;
 	}
	$('link_status').innerHTML = 'Checking the URL';
	new Ajax.Updater('link_status','/drizzo/engine/ajax/url_checker.php', {
		method: 'get',
		parameters: '?url=' + url,
	});
}

function warehouseConfirm() {
	if(confirm("Are you sure you want to remove the current version and replace it with the older one?")) {
		document.forms[0].submit();
	}
}

function getFolderContents(Folder) {
	$('file_list_div').innerHTML = '<div class="loading">Loading...</div>';
	new Ajax.Updater('file_list_div','/drizzo/engine/ajax/file_list.php', {
		method: 'get',
		parameters: '?top_dir=' + Folder,
	});
}

function insertMediaLink(linkType) {
	// Portions derived from http://parentnode.org/javascript/working-with-the-cursor-position/
	var URL = $('media_link_url').value;
	var fileName = $('media_link_name').value;
	var mimeType = $('media_link_type').value;
	if(linkType == 'embed') {
		if(mimeType.search('audio') >= 0) {
			var text = '<object type="' + mimeType + '" data="' + URL + '" width="50%" height="20"><param name="src" value="' + URL + '" /></object>';
		} else {
			var text = '<object type="' + mimeType + '" data="' + URL + '"><param name="src" value="' + URL + '" /></object>';
		}
	} else if(linkType == 'link'){
		var text = '<a href="' + URL + '" title="' + fileName + '">' + fileName + '</a>';
	}
	var browser = navigator.appName;
	if(browser == 'Microsoft Internet Explorer') { // For IE Browsers
		$('post_content').focus();
		var range = document.selection.createRange();
		if(range.parentElement() != 'post_content') {
			return false;
		}
		var orig = obj.value.replace(/rn/g, "n");
		range.text = text;
		var actual = tmp = $('post_content').value.replace(/rn/g, "n");
		for(var diff = 0; diff < orig.length; diff++) { 
		    if(orig.charAt(diff) != actual.charAt(diff)) break; 
		}
		for(var index = 0, start = 0; 
		    tmp.match(text) 
		        && (tmp = tmp.replace(text, "")) 
		        && index <= diff; 
		    index = start + text.length 
		) { 
		    start = actual.indexOf(text, index); 
		}
		$('post_content').value = $('post_content').value.substring(0,start) + text + $('post_content').value.substring(end,$('post_content').value.length);

    } else if(browser == 'Netscape') { // For Gecko Browsers (Firefox, Camino, etc)
		var start = $('post_content').selectionStart;
		var end = $('post_content').selectionEnd;
		var length = $('post_content').value.length;
		if((end == length) && (start != end)) {
			$('post_content').value = text;
		} else {
			$('post_content').value = $('post_content').value.substring(0,start) + text + $('post_content').value.substring(end,$('post_content').value.length);
		}
		$('post_content').selectionStart = Number($('post_content').value.substring(0,start).length)+ Number(text.length);
		$('post_content').selectionEnd = Number($('post_content').value.substring(0,start).length) + Number(text.length);

    } else { // For non-IE or Gecko Browsers
		$('post_content').value += text;
    }

	// Reset the hidden values and close the dialog box
	$('media_link_name').value = '';
	$('media_link_url').value = '';
	$('media_link_type').value = '';
	$('confirm_image').style.display = 'none';
	$('confirm_embed').style.display = 'none';
}

function insertImageLink(mimeType) {
	var URL = $('media_link_url').value;
	var fileName = $('media_link_name').value;
	var thumbURL = URL.replace('gallery/','gallery/thumbs/');
	var browser = navigator.appName;
	if(mimeType == 'image') {
		var text = '<img src="' + URL + '" alt="' + fileName + '" />';
	} else {
		var text = '<a href="' + URL + '" title="' + fileName + '"><img src="' + thumbURL + '" alt="' + fileName + '" /></a>';
	}
	
	if(browser == 'Microsoft Internet Explorer') { // For IE Browsers
		$('post_content').focus();
		var range = document.selection.createRange();
		if(range.parentElement() != 'post_content') {
			return false;
		}
		var orig = obj.value.replace(/rn/g, "n");
		range.text = text;
		var actual = tmp = $('post_content').value.replace(/rn/g, "n");
		for(var diff = 0; diff < orig.length; diff++) { 
		    if(orig.charAt(diff) != actual.charAt(diff)) break; 
		}
		for(var index = 0, start = 0; 
		    tmp.match(text) 
		        && (tmp = tmp.replace(text, "")) 
		        && index <= diff; 
		    index = start + text.length 
		) { 
		    start = actual.indexOf(text, index); 
		}
		$('post_content').value = $('post_content').value.substring(0,start) + text + $('post_content').value.substring(end,$('post_content').value.length);

	    } else if(browser == 'Netscape') { // For Gecko Browsers (Firefox, Camino, etc)
		var start = $('post_content').selectionStart;
		var end = $('post_content').selectionEnd;
		$('post_content').value = $('post_content').value.substring(0,start) + text + $('post_content').value.substring(end,$('post_content').value.length);
		$('post_content').selectionStart = Number($('post_content').value.substring(0,start).length)+ Number(text.length);
		$('post_content').selectionEnd = Number($('post_content').value.substring(0,start).length) + Number(text.length);

    } else { // For non-IE or Gecko Browsers
		$('post_content').value += text;
    }
	
	// Reset the hidden values and close the dialog box
	$('media_link_name').value = '';
	$('media_link_url').value = '';
	$('media_link_type').value = '';
	$('confirm_image').style.display = 'none';
	$('confirm_embed').style.display = 'none';
}

function mediaLinkConfirm(URL,fileName,mimeType) {
	if($('media_link_url') == null) {
		$('confirm_image').innerHTML += '<input type="hidden" id="media_link_url" value="' + URL + '" />';
	} else {
		$('media_link_url').value = URL;
	}
	
	if($('media_link_name') == null) {
		$('confirm_image').innerHTML += '<input type="hidden" id="media_link_name" value="' + fileName +'" />';
	} else {
		$('media_link_name').value = fileName;
	}

	if($('media_link_type') == null) {
		$('confirm_image').innerHTML += '<input type="hidden" id="media_link_type" value="' + mimeType + '" />';
	} else {
		$('media_link_type').value = mimeType;
	}
	if(mimeType.search('image') >= 0) {
		$('confirm_image').style.display = 'block';
	} else {
		$('confirm_embed').style.display = 'block';
	}
}

function updateDateDropdowns(name_prefix) {
	if(name_prefix == null) {
		var month = $('date_month_dropdown').options[$('date_month_dropdown').selectedIndex].value;
		var current_day = $('date_day_dropdown').options[$('date_day_dropdown').selectedIndex].value;
		var current_year = $('date_year_dropdown').options[$('date_year_dropdown').selectedIndex].value;
		var current_hour = $('date_hour_dropdown').options[$('date_hour_dropdown').selectedIndex].value;
		var current_min = $('date_min_dropdown').options[$('date_min_dropdown').selectedIndex].value;
		new Ajax.Updater('date_days','/drizzo/engine/ajax/days_dropdown.php',{
			method: 'get',
			parameters: '?month=' + month + '&current_day=' + current_day + '&current_hour=' + current_hour + '&current_year=' + current_year + '&current_min=' + current_min,
		});
		new Ajax.Updater('date_hour','/drizzo/engine/ajax/hours_dropdown.php',{
			method: 'get',
			parameters: '?month=' + month + '&current_day=' + current_day + '&current_hour=' + current_hour + '&current_year=' + current_year + '&current_min=' + current_min,
		});
	} else if(name_prefix == 'start_') {
		var month = $('start_date_month_dropdown').options[$('start_date_month_dropdown').selectedIndex].value;
		var current_day = $('start_date_day_dropdown').options[$('start_date_day_dropdown').selectedIndex].value;
		var current_year = $('start_date_year_dropdown').options[$('start_date_year_dropdown').selectedIndex].value;
		var current_hour = $('start_date_hour_dropdown').options[$('start_date_hour_dropdown').selectedIndex].value;
		var current_min = $('start_date_min_dropdown').options[$('start_date_min_dropdown').selectedIndex].value;
		new Ajax.Updater('start_date_days','/drizzo/engine/ajax/days_dropdown.php',{
			method: 'get',
			parameters: '?month=' + month + '&current_day=' + current_day + '&current_hour=' + current_hour + '&current_year=' + current_year + '&current_min=' + current_min + '&name_prefix=' + name_prefix,
		});
		new Ajax.Updater('start_date_hour','/drizzo/engine/ajax/hours_dropdown.php',{
			method: 'get',
			parameters: '?month=' + month + '&current_day=' + current_day + '&current_hour=' + current_hour + '&current_year=' + current_year + '&current_min=' + current_min + '&name_prefix=' + name_prefix,
		});
	} else if(name_prefix == 'end_') {
		var month = $('end_date_month_dropdown').options[$('end_date_month_dropdown').selectedIndex].value;
		var current_day = $('end_date_day_dropdown').options[$('end_date_day_dropdown').selectedIndex].value;
		var current_year = $('end_date_year_dropdown').options[$('end_date_year_dropdown').selectedIndex].value;
		var current_hour = $('end_date_hour_dropdown').options[$('end_date_hour_dropdown').selectedIndex].value;
		var current_min = $('end_date_min_dropdown').options[$('end_date_min_dropdown').selectedIndex].value;
		new Ajax.Updater('end_date_days','/drizzo/engine/ajax/days_dropdown.php',{
			method: 'get',
			parameters: '?month=' + month + '&current_day=' + current_day + '&current_hour=' + current_hour + '&current_year=' + current_year + '&current_min=' + current_min + '&name_prefix=' + name_prefix,
		});
		new Ajax.Updater('end_date_hour','/drizzo/engine/ajax/hours_dropdown.php',{
			method: 'get',
			parameters: '?month=' + month + '&current_day=' + current_day + '&current_hour=' + current_hour + '&current_year=' + current_year + '&current_min=' + current_min + '&name_prefix=' + name_prefix,
		});
	}
}

function galleryMetaCheck(parent_dir) {
	if(parent_dir.search('gallery') >= 0 && $('gallery_meta').style.display == 'none') {
		new Effect.BlindDown('gallery_meta');
	} else if(parent_dir.search('gallery') < 0 && $('gallery_meta').style.display != 'none') {
		new Effect.BlindUp('gallery_meta');
	}
}

function allDayEvent() {
	var all_day = $('all_day').checked;
	if(all_day) {
		$('start_date_hour_dropdown').selectedIndex = 0;
		$('start_date_min_dropdown').selectedIndex = 0;
		$('start_date_meridian_dropdown').selectedIndex = 0;
		$('end_date_hour_dropdown').selectedIndex = 23;
		$('end_date_min_dropdown').selectedIndex = 59;
		$('end_date_meridian_dropdown').selectedIndex = 1;
	}
}

function checkDateRange() {
	var start=new Date($('start_date_year_dropdown').selectedIndex,$('start_date_month_dropdown').selectedIndex,$('start_date_day_dropdown').selectedIndex,$('start_date_hour_dropdown').selectedIndex,$('start_date_min_dropdown').selectedIndex,'00');
	var end=new Date($('end_date_year_dropdown').selectedIndex,$('end_date_month_dropdown').selectedIndex,$('end_date_day_dropdown').selectedIndex,$('end_date_hour_dropdown').selectedIndex,$('end_date_min_dropdown').selectedIndex,'00');
	if(start <= end) {
		return true;
	} else {
		alert("The End Date is before the Start Date");
		return false;
	}
}

function setTimezoneOffset() {
	var offset = Date().substr(Number(Date().search('GMT')) + 3, Number(Date().search('GMT')) + 8)
	var hours = offset.substr(0,3);
	var minutes = Number(offset.substr(3,2)) / 60;
	offset = hours + '.' + minutes;
	if(document.cookie.indexOf('drizzo_timezone') != offset) {
		document.cookie = 'drizzo_timezone=' + offset + ';path=/';
	}
}
